<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Send extends Model
{
    protected $fillable = [
        "title",
        "content",
        "type",
        "send_to_id",
        "send_to_able",
        "admin_id",
        "has_exception",
        "excpetion_msg"
    ];

    const TYPE_SMS = "sms";
    const TYPE_EMAIL = "email";

    public static $mapTYPE = [
        self::TYPE_EMAIL => "邮件",
        self::TYPE_SMS => "短信"
    ];

    protected $casts = [
        "has_exception" => "boolean",
        "exception_msg" => "json"
    ];

    /**
     * 保存发送短信记录
     * @param int $to_id 发送给谁
     * @param string $to_type 发送人的类型   管理员|用户
     * @param string $title 发送的标题
     * @param string $content 发送的内容
     * @param string $type 发送类型       短信|邮件 (需要扩展: 比如api发送等... 请在Send中编写常量)
     */
    public function saveSmsHistory($to_id, $to_type, $title, $content, $type = Send::TYPE_SMS)
    {
        self::query()->create(
            [
                "title" => $title,
                "content" => $content,
                "type" => $type,
                "send_to_id" => $to_id,
                "send_to_able" => $to_type,
                "admin_id" => Auth::guard("admin")->id(),
            ]
        );
    }
}
